﻿#include "UserModel.h"
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
const std::string UserModel::s_userInfoPath = "userinfo.xml";

UserModel::UserModel()
{
    readUserFromFile();
}

const UserInfo& UserModel::getUserInfo() const
{
    return m_userInfo;
}

bool UserModel::readUserFromFile()
{
    return m_userInfo.loadXmlFromFile(UserModel::s_userInfoPath);
}

bool UserModel::saveUser2File(const UserInfo& playerInfo)
{
    m_userInfo = playerInfo;
    FILE* saveFile = fopen(UserModel::s_userInfoPath.c_str(), "wb");
    if (saveFile)
    {
        fputs(m_userInfo.getXml().c_str(), saveFile);
        fclose(saveFile);
        saveFile = nullptr;
    }
    return false;
}

bool UserModel::hasUser()
{
    std::fstream userPath(UserModel::s_userInfoPath);
    return userPath.good();
}
